#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   75633-sestatus.sh
# Version:    1.0
# Date:       2013/12/17
# Author:     yuanhui.shi
# Email:      yuanhui.shi@cs2c.com.cn
# Summary:    03系统安全功能-01SELinux-03SELinux重要命令-01SELinux运行状态-02sestatus状态和策略查询
# Notes:      sestatus
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/17
#             -   The first one
# ----------------------------------------------------------------------

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../lib/Echo.sh
source ../../lib/XmlParse.sh


function CleanData ()
{
  EchoInfo "75633-sestatus.sh执行完毕"
}

trap "CleanData" EXIT

SELINUX_STATUS=`getenforce |tr 'A-Z' 'a-z'`
FILE_STATUS=`cat /etc/selinux/config|grep -v "#"|grep SELINUX=|cut -d "=" -f2`
CURRENT_MODE=`sestatus |grep Current |cut -d " " -f21`
FILE_MODE=`sestatus |grep "Mode from config file" |cut -d " " -f14`
SELINUX_MODE=`sestatus |head -1|cut -d " " -f19`

EchoInfo "判断命令sestatus的结果是否正确"
if [ "$SELINUX_STATUS" == "$SELINUX_MODE" ];then  
       echo "当关闭SELinux时，sestatus命令查询结果正确"
elif [ "$SELINUX_MODE" == "enabled" ];then
       if [ "$FILE_MODE" == "$FILE_STATUS" -a "$CURRENT_MODE" == "$SELINUX_STATUS" ];then
           echo "当开启SELinux时，sestatus命令查询结果正确"
       fi
else 
       exit 1
fi


EchoInfo "判断命令sestatus -v 的结果是否正确"

if [ "$SELINUX_STATUS" == "disabled" ];then
   if [ `sestatus` == `sestatus -v` ];then
          echo "当关闭SELinux时，sestatus -v命令查询结果正确"   
   fi
elif [ "$SELINUX_STATUS" == "permissive" -o "$SELINUX_STATUS" == "enforcing" ];then
         if [ -n "`sestatus -v |grep contexts`" ];then
             echo "当开启SELinux时，sestatus -v命令查询结果正确"
         fi
else
        exit 1
fi

